package game.gol;

public class Cell
{
    private boolean alive;
    private boolean surviving;
    // neighbors
    private Cell topLeft;
    private Cell top;
    private Cell topRight;
    private Cell left;
    // this
    private Cell right;
    private Cell bottomLeft;
    private Cell bottom;
    private Cell bottomRight;
    
    public Cell(boolean alive, boolean surviving)
    {
        this.setAlive(alive);
        this.setSurviving(surviving);
    }
    
    public boolean isAlive()
    {
        return alive;
    }
    
    public void setAlive(boolean alive)
    {
        this.alive = alive;
    }
    
    public boolean isSurviving()
    {
        return surviving;
    }
    
    public void setSurviving(boolean surviving)
    {
        this.surviving = surviving;
    }
    
    public Cell getTopLeft()
    {
        return topLeft;
    }

    public void setTopLeft(Cell topLeft)
    {
        this.topLeft = topLeft;
    }

    public Cell getTop()
    {
        return top;
    }

    public void setTop(Cell top)
    {
        this.top = top;
    }

    public Cell getTopRight()
    {
        return topRight;
    }

    public void setTopRight(Cell topRight)
    {
        this.topRight = topRight;
    }

    public Cell getLeft()
    {
        return left;
    }

    public void setLeft(Cell left)
    {
        this.left = left;
    }

    public Cell getRight()
    {
        return right;
    }

    public void setRight(Cell right)
    {
        this.right = right;
    }

    public Cell getBottomLeft()
    {
        return bottomLeft;
    }

    public void setBottomLeft(Cell bottomLeft)
    {
        this.bottomLeft = bottomLeft;
    }

    public Cell getBottom()
    {
        return bottom;
    }

    public void setBottom(Cell bottom)
    {
        this.bottom = bottom;
    }

    public Cell getBottomRight()
    {
        return bottomRight;
    }

    public void setBottomRight(Cell bottomRight)
    {
        this.bottomRight = bottomRight;
    }
    
    public int countLivingNeighbors()
    {
        int livingNeighbors = 0;
        if (topLeft.isAlive())
        {
            livingNeighbors++;
        }
        if (top.isAlive())
        {
            livingNeighbors++;
        }
        if (topRight.isAlive())
        {
            livingNeighbors++;
        }
        if (left.isAlive())
        {
            livingNeighbors++;
        }
        if (right.isAlive())
        {
            livingNeighbors++;
        }
        if (bottomLeft.isAlive())
        {
            livingNeighbors++;
        }
        if (bottom.isAlive())
        {
            livingNeighbors++;
        }
        if (bottomRight.isAlive())
        {
            livingNeighbors++;
        }
        
        return livingNeighbors;
    }

    @Override
    public String toString()
    {
        StringBuilder s = new StringBuilder();
        s.append("alive=").append(isAlive());
        s.append("; surviving=").append(isSurviving());
        return s.toString();
    }
}
